using System;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Text;
using log4net;
using System.IO;

namespace Utility
{

    public sealed class EmailUtility
    {

        private static log4net.ILog logger;
        private static char[] charSeparators;
        static EmailUtility()
        {
            EmailUtility.charSeparators = new char[] { };
            EmailUtility.logger = LoggingUtility.GetLogger(typeof(EmailUtility).FullName);
        }

        public EmailUtility()
        {
        }

        public static bool Send(string smtpHost, string from, string[] to, string subject, string message)
        {
            bool flag1 = true, flag2 = true;

            try
            {
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                int i = 0;
                while (flag2)
                {
                    mailMessage.To.Add(new System.Net.Mail.MailAddress(to[i]));
                    i++;
                    flag2 = i < to.Length;
                }
                mailMessage.From = new System.Net.Mail.MailAddress(from);
                mailMessage.Subject = subject;
                mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpHost);
                smtpClient.Send(mailMessage);
                flag1 = true;
            }
            catch (System.Exception e)
            {
                flag2 = !EmailUtility.logger.IsErrorEnabled;
                if (!flag2)
                    EmailUtility.logger.Error(e.Message);
                flag1 = false;
            }
            return flag1;
        }

        public static bool Send(string smtpHost, string from, string to, string subject, string message, string[] cc, string[] bcc)
        {
            bool flag1, flag2;

            try
            {
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                string s = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\uFFFD";
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(s);
                flag2 = !regex.IsMatch(to);
                if (!flag2)
                    mailMessage.To.Add(new System.Net.Mail.MailAddress(to));
                int i1 = 0;
                while (flag2)
                {
                    flag2 = !regex.IsMatch(cc[i1]);
                    if (!flag2)
                        mailMessage.CC.Add(new System.Net.Mail.MailAddress(cc[i1]));
                    i1++;
                    flag2 = i1 < cc.Length;
                }
                int i2 = 0;
                while (flag2)
                {
                    flag2 = !regex.IsMatch(bcc[i2]);
                    if (!flag2)
                        mailMessage.Bcc.Add(new System.Net.Mail.MailAddress(bcc[i2]));
                    i2++;
                    flag2 = i2 < bcc.Length;
                }
                mailMessage.From = new System.Net.Mail.MailAddress(from);
                mailMessage.Subject = subject;
                mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpHost);
                smtpClient.Send(mailMessage);
                flag1 = true;
            }
            catch (System.Exception e)
            {
                flag2 = !EmailUtility.logger.IsErrorEnabled;
                if (!flag2)
                    EmailUtility.logger.Error(e.Message);
                flag1 = false;
            }
            return flag1;
        }

        public static bool Send(string smtpHost, string from, string to, string subject, string message, params string[] cc)
        {
            bool flag1 = true, flag2 = true;

            try
            {
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                mailMessage.To.Add(new System.Net.Mail.MailAddress(to));
                int i = 0;
                while (flag2)
                {
                    mailMessage.CC.Add(new System.Net.Mail.MailAddress(cc[i]));
                    i++;
                    flag2 = i < cc.Length;
                }
                mailMessage.From = new System.Net.Mail.MailAddress(from);
                mailMessage.Subject = subject;
                mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpHost);
                smtpClient.Send(mailMessage);
                flag1 = true;
            }
            catch (System.Exception e)
            {
                flag2 = !EmailUtility.logger.IsErrorEnabled;
                if (!flag2)
                    EmailUtility.logger.Error(e.Message);
                flag1 = false;
            }
            return flag1;
        }

        public static bool Send(string smtpHost, string from, string to, string subject, string message)
        {
            bool flag1;

            try
            {
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                mailMessage.To.Add(new System.Net.Mail.MailAddress(to));
                mailMessage.From = new System.Net.Mail.MailAddress(from);
                mailMessage.Subject = subject;
                mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpHost);
                smtpClient.Send(mailMessage);
                flag1 = true;
            }
            catch (System.Exception e)
            {
                bool flag2 = !EmailUtility.logger.IsErrorEnabled;
                if (!flag2)
                    EmailUtility.logger.Error(e.Message);
                flag1 = false;
            }
            return flag1;
        }
        public static string GetMailTemplate(string filePath)
        {
            string body = string.Empty;
            if (File.Exists(filePath))
            {
                StreamReader srReadLine = new StreamReader(filePath, System.Text.Encoding.GetEncoding("UTF-8"));
                srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);

                while (true)
                {
                    string str = srReadLine.ReadLine();
                    if (str == null)
                        break;
                    else
                        body += str + "\r\n";
                }
                srReadLine.Close();
            }
            return body;
        }

    }
    public class MailMessage1
    {
        #region Fields
        private static log4net.ILog logger;
        private string serverName;
        private string username;
        private string password;
        private string subject;
        private string body;
        private string from;
        private string to;
        private string cc;
        private string bcc;
        private string logFile;

        #endregion Fields

        #region Contructors
        static MailMessage1()
        {
            logger = LoggingUtility.GetLogger(typeof(EmailUtility).FullName);
        }

        public MailMessage1()
        {
            this.serverName = string.Empty;
            this.username = string.Empty;
            this.password = string.Empty;
            this.body = string.Empty;
            this.from = string.Empty;
            this.to = string.Empty;
            this.cc = string.Empty;
            this.bcc = string.Empty;
            this.subject = string.Empty;
            this.logFile = string.Empty;
        }
        public MailMessage1(string serverName, string username, string password, string from, string to, string cc, string bcc, string subject, string body)
        {
            this.serverName = serverName;
            this.username = username;
            this.password = password;
            this.body = body;
            this.from = from;
            this.to = to.Replace(",", ";");
            this.cc = cc.Replace(",", ";");
            this.bcc = bcc.Replace(",", ";");
            this.subject = subject;
        }
        #endregion Contructors

        #region Methods

        public bool SendMail()
        {

            SmtpClient client = new SmtpClient();
            client.Timeout = 60000;
            client.Host = this.serverName;
            client.Port = 25;
            if (this.username != string.Empty)
            {
                NetworkCredential cred = new NetworkCredential(this.username, this.password);
                client.Credentials = cred;
            }
            // Make an mail message to send over smtp client
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
            if (this.from != string.Empty)
                mailMessage.From = new MailAddress(this.from);
            if (this.to != string.Empty)
            {
                string[] strto = this.to.Split(';');
                for (int i = 0; i < strto.Length; i++)
                {
                    mailMessage.To.Add(strto[i].ToString());
                }
            }
            if (this.cc != string.Empty)
            {
                string[] strcc = this.cc.Split(';');
                for (int i = 0; i < strcc.Length; i++)
                {
                    mailMessage.CC.Add(strcc[i].ToString());
                }
            }
            if (this.bcc != string.Empty)
            {
                string[] strbcc = this.bcc.Split(';');
                for (int i = 0; i < strbcc.Length; i++)
                {
                    mailMessage.Bcc.Add(strbcc[i].ToString());
                }
            }
            mailMessage.Subject = this.subject;
            mailMessage.Body = this.body;

            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.Headers.Add("X-Company", "Company");
            mailMessage.Headers.Add("X-Location", "Vietnamese");
            try
            {
                client.Send(mailMessage);
                mailMessage.Dispose();
                return true;
            }
            catch (Exception e)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(e.Message);
                }
                return false;
            }
        }
        #endregion Methods

        #region Properties
        public string ServerName
        {
            get
            {
                return this.serverName;
            }
            set
            {
                this.serverName = value;
            }
        }
        public string Username
        {
            get
            {
                return this.username;
            }
            set
            {
                this.username = value;
            }
        }
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
            }
        }
        public string From
        {
            get
            {
                return this.from;
            }
            set
            {
                this.from = value;
            }
        }
        public string To
        {
            get
            {
                return this.to;
            }
            set
            {
                this.to = value;
            }
        }
        public string Subject
        {
            get
            {
                return this.subject;
            }
            set
            {
                this.subject = value;
            }
        }
        public string Body
        {
            get
            {
                return this.body;
            }
            set
            {
                this.body = value;
            }
        }
        public string Bcc
        {
            get
            {
                return this.bcc;
            }
            set
            {
                this.bcc = value;
            }
        }
        public string Cc
        {
            get
            {
                return this.cc;
            }
            set
            {
                this.cc = value;
            }
        }
        public string LogFile
        {
            get
            {
                return this.logFile;
            }
            set
            {
                this.logFile = value;
            }
        }
        #endregion Properties

    }


}

